<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx负载均衡机制详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
            background-color: #f9fafb;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
            color: white;
            position: relative;
            overflow: hidden;
        }
        .hero::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('');
            opacity: 0.3;
        }
        .hero-content {
            position: relative;
            z-index: 1;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .content-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .content-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .code-block {
            background: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            position: relative;
        }
        .code-header {
            background: #1e1e1e;
            border-radius: 8px 8px 0 0;
            padding: 8px 12px;
            color: #d4d4d4;
            font-size: 0.9rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .copy-btn {
            background: none;
            border: none;
            color: #d4d4d4;
            cursor: pointer;
            transition: color 0.2s;
        }
        .copy-btn:hover {
            color: white;
        }
        .nav-link {
            position: relative;
            padding-bottom: 4px;
        }
        .nav-link::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 0;
            height: 2px;
            background: #4f46e5;
            transition: width 0.3s ease;
        }
        .nav-link:hover::after {
            width: 100%;
        }
        .algorithm-card {
            border-left: 4px solid #4f46e5;
            transition: all 0.3s ease;
        }
        .algorithm-card:hover {
            border-left-width: 8px;
            background-color: #f5f3ff;
        }
        footer {
            background: #1a1a1a;
            color: #e5e7eb;
        }
        footer a:hover {
            color: #a5b4fc;
        }
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero py-20 md:py-32">
        <div class="container mx-auto px-6 md:px-12 hero-content">
            <div class="max-w-4xl mx-auto text-center">
                <div class="inline-flex items-center px-4 py-2 rounded-full bg-white bg-opacity-20 text-sm font-medium mb-6">
                    <i class="fas fa-server mr-2"></i>
                    <span>高性能Web服务</span>
                </div>
                <h1 class="text-4xl md:text-6xl font-bold mb-6 leading-tight">Nginx负载均衡机制详解</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8 max-w-3xl mx-auto">通过不同的算法和策略，构建灵活且高效的流量分配方案</p>
                <div class="flex justify-center space-x-4">
                    <a href="#mechanism" class="bg-white text-indigo-600 px-6 py-3 rounded-lg font-medium hover:bg-opacity-90 transition-all shadow-lg">
                        <i class="fas fa-cogs mr-2"></i>工作原理
                    </a>
                    <a href="#algorithms" class="bg-indigo-800 text-white px-6 py-3 rounded-lg font-medium hover:bg-indigo-700 transition-all shadow-lg">
                        <i class="fas fa-random mr-2"></i>算法解析
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 md:px-12 py-12">
        <!-- Mechanism Section -->
        <section id="mechanism" class="mb-20">
            <div class="flex items-center mb-12">
                <div class="h-1 w-12 bg-indigo-500 rounded-full mr-4"></div>
                <h2 class="text-3xl font-bold">负载均衡机制的工作原理</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-8">
                <!-- Card 1 -->
                <div class="content-card p-8">
                    <div class="text-indigo-500 text-4xl mb-4">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-4">请求分发</h3>
                    <p class="text-gray-600">Nginx 作为反向代理服务器，当接收到客户端的请求时，它根据配置的负载均衡算法将请求分发到后端服务器池中的一台或多台服务器。</p>
                </div>
                
                <!-- Card 2 -->
                <div class="content-card p-8">
                    <div class="text-indigo-500 text-4xl mb-4">
                        <i class="fas fa-heartbeat"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-4">健康检查</h3>
                    <p class="text-gray-600">Nginx 可以对后端服务器进行健康检查，以确保请求只发送到健康的服务器上。如果某台服务器出现故障或不响应，Nginx 会自动从负载均衡池中移除该服务器。</p>
                </div>
                
                <!-- Card 3 -->
                <div class="content-card p-8">
                    <div class="text-indigo-500 text-4xl mb-4">
                        <i class="fas fa-route"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-4">请求路由</h3>
                    <p class="text-gray-600">Nginx 通过配置不同的路由规则和策略来决定请求如何被分发。负载均衡机制可以根据各种条件（如客户端 IP、请求 URI、请求头等）来决定如何处理请求。</p>
                </div>
            </div>
            
            <!-- Visualization -->
            <div class="mt-16 content-card p-6">
                <div class="mermaid">
                    graph LR
                    A[客户端请求] --> B[Nginx负载均衡器]
                    B --> C1[服务器1]
                    B --> C2[服务器2]
                    B --> C3[服务器3]
                    C1 --> D[响应]
                    C2 --> D
                    C3 --> D
                    D --> A
                    style B fill:#4f46e5,color:white,stroke:#4f46e5
                    style A fill:#10b981,color:white,stroke:#10b981
                    style C1 fill:#3b82f6,color:white,stroke:#3b82f6
                    style C2 fill:#3b82f6,color:white,stroke:#3b82f6
                    style C3 fill:#3b82f6,color:white,stroke:#3b82f6
                    style D fill:#10b981,color:white,stroke:#10b981
                </div>
            </div>
        </section>

        <!-- Algorithms Section -->
        <section id="algorithms" class="mb-20">
            <div class="flex items-center mb-12">
                <div class="h-1 w-12 bg-indigo-500 rounded-full mr-4"></div>
                <h2 class="text-3xl font-bold">常见的负载均衡算法</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Round Robin -->
                <div class="algorithm-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 p-2 rounded-full mr-4">
                            <i class="fas fa-redo-alt text-indigo-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">轮询 (Round Robin)</h3>
                    </div>
                    <p class="text-gray-600 mb-4">这是默认的负载均衡算法，它将请求依次分发到后端服务器池中的每台服务器。这种算法简单且公平，适用于后端服务器处理能力相似的场景。</p>
                    
                    <div class="code-block mt-4">
                        <div class="code-header">
                            <span>nginx.conf</span>
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="p-4 overflow-x-auto"><code>upstream backend {
    server backend1.toString.com;
    server backend2.toString.com;
    server backend3.toString.com;
}</code></pre>
                    </div>
                </div>
                
                <!-- Least Connections -->
                <div class="algorithm-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 p-2 rounded-full mr-4">
                            <i class="fas fa-project-diagram text-indigo-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">最少连接 (Least Connections)</h3>
                    </div>
                    <p class="text-gray-600 mb-4">该算法将请求分发到当前连接数最少的后端服务器。它适用于负载不均或某些请求处理时间较长的情况，可以帮助更均衡地利用服务器资源。</p>
                    
                    <div class="code-block mt-4">
                        <div class="code-header">
                            <span>nginx.conf</span>
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="p-4 overflow-x-auto"><code>upstream backend {
    least_conn;
    server backend1.toString.com;
    server backend2.toString.com;
    server backend3.toString.com;
}</code></pre>
                    </div>
                </div>
                
                <!-- IP Hash -->
                <div class="algorithm-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 p-2 rounded-full mr-4">
                            <i class="fas fa-fingerprint text-indigo-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">IP 哈希 (IP Hash)</h3>
                    </div>
                    <p class="text-gray-600 mb-4">该算法通过计算客户端 IP 地址的哈希值，将相同 IP 的请求始终发送到相同的后端服务器。适用于需要保持会话粘性（session stickiness）的应用场景。</p>
                    
                    <div class="code-block mt-4">
                        <div class="code-header">
                            <span>nginx.conf</span>
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="p-4 overflow-x-auto"><code>upstream backend {
    ip_hash;
    server backend1.toString.com;
    server backend2.toString.com;
    server backend3.toString.com;
}</code></pre>
                    </div>
                </div>
                
                <!-- Weighted Round Robin -->
                <div class="algorithm-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 p-2 rounded-full mr-4">
                            <i class="fas fa-balance-scale-right text-indigo-600"></i>
                        </div>
                        <h3 class="text-xl font-semibold">加权轮询 (Weighted Round Robin)</h3>
                    </div>
                    <p class="text-gray-600 mb-4">该算法为每台后端服务器分配一个权重值，根据权重值来调整请求的分配比例。权重较大的服务器会接收更多的请求。适用于后端服务器处理能力不同的场景。</p>
                    
                    <div class="code-block mt-4">
                        <div class="code-header">
                            <span>nginx.conf</span>
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="p-4 overflow-x-auto"><code>upstream backend {
    server backend1.toString.com weight=3;
    server backend2.toString.com weight=2;
    server backend3.toString.com weight=1;
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Advanced Configurations -->
        <section id="advanced" class="mb-20">
            <div class="flex items-center mb-12">
                <div class="h-1 w-12 bg-indigo-500 rounded-full mr-4"></div>
                <h2 class="text-3xl font-bold">高级负载均衡配置</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-8">
                <!-- Backup Server -->
                <div class="content-card p-8">
                    <div class="text-indigo-500 text-4xl mb-4">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-4">备份服务器</h3>
                    <p class="text-gray-600 mb-4">Nginx 允许配置备份服务器，这些服务器只有在所有主服务器都不可用时才会接收请求。</p>
                    
                    <div class="code-block mt-4">
                        <div class="code-header">
                            <span>nginx.conf</span>
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="p-4 overflow-x-auto"><code>upstream backend {
    server backend1.toString.com;
    server backend2.toString.com backup;
}</code></pre>
                    </div>
                </div>
                
                <!-- Health Check -->
                <div class="content-card p-8">
                    <div class="text-indigo-500 text-4xl mb-4">
                        <i class="fas fa-heartbeat"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-4">健康检查 (Nginx Plus)</h3>
                    <p class="text-gray-600 mb-4">Nginx Plus 提供了内置的健康检查功能，可以对后端服务器进行主动健康检查，以自动处理服务器故障和恢复。</p>
                    
                    <div class="code-block mt-4">
                        <div class="code-header">
                            <span>nginx.conf</span>
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="p-4 overflow-x-auto"><code>upstream backend {
    server backend1.toString.com;
    server backend2.toString.com;
    server backend3.toString.com;

    # Nginx Plus only
    health_check;
}</code></pre>
                    </div>
                </div>
                
                <!-- Session Stickiness -->
                <div class="content-card p-8">
                    <div class="text-indigo-500 text-4xl mb-4">
                        <i class="fas fa-link"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-4">会话粘性 (Nginx Plus)</h3>
                    <p class="text-gray-600 mb-4">可以使用 sticky 模块来实现会话粘性，确保相同的用户请求始终发送到同一台服务器。</p>
                    
                    <div class="code-block mt-4">
                        <div class="code-header">
                            <span>nginx.conf</span>
                            <button class="copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="p-4 overflow-x-auto"><code>upstream backend {
    sticky;
    server backend1.toString.com;
    server backend2.toString.com;
    server backend3.toString.com;
}</code></pre>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Algorithm Comparison -->
        <section class="content-card p-8 mb-20">
            <h3 class="text-2xl font-semibold mb-6 text-center">负载均衡算法比较</h3>
            <div class="mermaid">
                gantt
                title 负载均衡算法适用场景比较
                dateFormat  X
                axisFormat %s
                section 算法特点
                轮询 :a1, 0, 30
                最少连接 :a2, 0, 30
                IP哈希 :a3, 0, 30
                加权轮询 :a4, 0, 30
                
                section 适用场景
                服务器性能相似 :crit, 0, 10
                请求处理时间差异大 :crit, 10, 20
                需要会话保持 :crit, 20, 30
                服务器性能不同 :crit, 0, 30
                
                section 算法对应
                a1 : 服务器性能相似
                a2 : 请求处理时间差异大
                a3 : 需要会话保持
                a4 : 服务器性能不同
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="py-12">
        <div class="container mx-auto px-6 md:px-12">
            <div class="flex flex-col items-center">
                <div class="text-2xl font-bold mb-2">技术小馆</div>
                <div class="text-gray-400 mb-6">专注于高质量技术内容分享</div>
                <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-indigo-300 transition-colors">
                    <i class="fas fa-globe mr-2"></i> www.yuque.com/jtostring
                </a>
                <div class="mt-8 text-gray-500 text-sm">
                    &copy; 2023 技术小馆. All rights reserved.
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            gantt: {
                barHeight: 30,
                fontSize: 14
            }
        });
        
        // Copy code functionality
        function copyCode(button) {
            const codeBlock = button.closest('.code-block');
            const code = codeBlock.querySelector('code').innerText;
            navigator.clipboard.writeText(code).then(() => {
                const icon = button.querySelector('i');
                icon.classList.remove('far', 'fa-copy');
                icon.classList.add('fas', 'fa-check');
                setTimeout(() => {
                    icon.classList.remove('fas', 'fa-check');
                    icon.classList.add('far', 'fa-copy');
                }, 2000);
            });
        }
    </script>
</body>
</html>